package com.wxq.yaoxia.drug;


import com.wxq.yaoxia.R;
import com.wxq.yaoxia.YXBaseActivity;
import com.wxq.yaoxia.mainborad.YXMainboradActivity;
import com.wxq.yaoxia.util.YXConstants;
import com.wxq.yaoxia.util.YXDBHelper;

import android.app.SearchManager;
import android.app.SearchManager.OnDismissListener;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class YXDurgSearchActivity extends YXBaseActivity {

	ListView commonLV;
	String usefor;
	ImageButton searchBar;
	TextView hipTV;
	
	private YXDBHelper dbHelper;
	Cursor cursor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dbHelper = new YXDBHelper(this);
		Intent oldIntent = getIntent();
		usefor = oldIntent.getStringExtra(YXConstants.YX_USE);
		
		setContentViewWithTopbar(R.layout.yx_search, R.layout.yx_topbar);
		hipTV = (TextView) findViewById(R.id.hipTV);
		if(YXConstants.YX_USE_DRUG.equals(usefor))
		{
			hipTV.setText("常用药品 ");
		}
		else if (YXConstants.YX_USE_DISEASE.equals(usefor)) {
			hipTV.setText("常见疾病 ");
		}
		searchBar = (ImageButton) findViewById(R.id.searchBar);
		searchBar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				onSearchRequested();
			}
		});
		
		commonLV = (ListView) findViewById(R.id.commonLV);
		setCommonListView();
		commonLV.setOnItemClickListener(
				new OnItemClickListener()
				{

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// TODO Auto-generated method stub
						cursor.moveToPosition(position);
						Intent newIntent = new Intent();
						newIntent.putExtra(YXConstants.KEY_ID, cursor.getLong(cursor.getColumnIndex(YXConstants.KEY_ID)));
						newIntent.putExtra(YXConstants.COLUMN_Name, cursor.getString(cursor.getColumnIndex(YXConstants.COLUMN_Name)));
						newIntent.putExtra(YXConstants.DRUG_COLUMN_BrandName, cursor.getString(cursor.getColumnIndex(YXConstants.DRUG_COLUMN_BrandName)));
						newIntent.putExtra(YXConstants.DRUG_COLUMN_CommonName, cursor.getString(cursor.getColumnIndex(YXConstants.DRUG_COLUMN_CommonName)));
						newIntent.putExtra(YXConstants.COLUMN_QueryId, cursor.getString(cursor.getColumnIndex(YXConstants.COLUMN_QueryId)));
						newIntent.setClass(YXDurgSearchActivity.this, YXDrugDetailActivity.class);
						YXDurgSearchActivity.this.startActivity(newIntent);
					}
					
				});
	}
	
	

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//searchBar.setVisibility(View.GONE);
	}



	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//searchBar.setVisibility(View.VISIBLE);
	}



	@Override
	public void setTopBarButtonListener() {
		// TODO Auto-generated method stub
		Button leftBtn = (Button) findViewById(R.id.btnLeft);
    	Button rightBtnButton = (Button) findViewById(R.id.btnRight);
    	TextView centerTV = (TextView) findViewById(R.id.centerTV);
    	
    	leftBtn.setVisibility(View.VISIBLE);
    	leftBtn.setText("返回");
    	leftBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
    	rightBtnButton.setVisibility(View.VISIBLE);
//    	rightBtnButton.setText("药");
    	rightBtnButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(YXDurgSearchActivity.this, YXMainboradActivity.class);
				YXDurgSearchActivity.this.startActivity(intent);
			}
		});
    	centerTV.setVisibility(View.VISIBLE);
    	if(YXConstants.YX_USE_DRUG.equals(usefor))
    	{
    		centerTV.setText("药品");
    	}
    	else if (YXConstants.YX_USE_DISEASE.equals(usefor)) {
    		centerTV.setText("疾病");
		}
    	
	}
	
	public void setCommonListView() {
		cursor = dbHelper.getDrugsCommonUse("");
		startManagingCursor(cursor);
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				this, 
				R.layout.shool_item, 
				cursor, 
				new String [] {YXConstants.COLUMN_Name}, 
				new int [] {android.R.id.text1});
		commonLV.setAdapter(adapter);
	}
	
	@Override
	public boolean onSearchRequested() {
		
		
		((View) findViewById(R.id.list_search_overlay)).setVisibility(View.VISIBLE);
		 final SearchManager searchManager = (SearchManager) this.getSystemService(Context.SEARCH_SERVICE);
		 searchManager.setOnDismissListener(new OnDismissListener() {

			 @Override
			 public void onDismiss() 
			 {
				 ((View)findViewById(R.id.list_search_overlay)).setVisibility(View.GONE);
				 findViewById(R.id.searchBarLayout).setVisibility(View.VISIBLE);
			 }
		});
		 
		findViewById(R.id.searchBarLayout).setVisibility(View.GONE);

		Bundle appDataBundle = new Bundle();
		appDataBundle.putString("demo_key", "wxq传入的durgSearch");

		startSearch("", true, appDataBundle, false);
		return true;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(cursor != null && !cursor.isClosed())
		{
			cursor.close();
		}
		if(dbHelper != null)
		{
			dbHelper.close();
		}
		super.onDestroy();
	}
}
